class TaskController {

  def index = { }
  def list = {
    params.max = Math.min(params.max ? params.max.toInteger() : 10, 100)
    params.sort = params.sort ? params.sort : 'dateActive'
    params.order = params.order ? params.order : 'desc'
    [eventList: Event.list(params), total: Event.count()]
  }

  def recent = {
    def baby = Baby.get(params.id)
    params.max = Math.min(params.max ? params.max.toInteger() : 10, 100)
    params.sort = params.sort ? params.sort : 'dateActive'
    params.order = params.order ? params.order : 'desc'
    def total = Task.countByBaby(baby)
    [taskList: Task.findAllByBaby(baby, params), total: total]
  }

  def finish = {
    def task = Task.get(params.id)
    if (task) {
      task.status = Task.TASK_STATUS_DONE
      task.dateUpdate = new Date()
      task.save()
      def event = new Event()
      event.title = task.title
      event.content = task.content
      event.read = true
      event.baby = task.baby
      event.save()
      return render("0")
    } else {
      return render("2")
    }
  }

  def delete = {
    def taskInstance = Task.get(params.id)
    if (taskInstance) {
      try {
        taskInstance.delete()
        flash.message = "Task ${params.id} deleted"
        return render('0')
      }
      catch (org.springframework.dao.DataIntegrityViolationException e) {
        flash.message = "Catalog ${params.id} could not be deleted"
        return render("1")
      }
    }
    else {
      flash.message = "Catalog not found with id ${params.id}"
      return render("2")
    }
  }
}
